package com.twenty.seven.exception;

import com.twenty.seven.result.Result;
import com.twenty.seven.result.ResultCodeEnum;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.HttpClientErrorException;

@ControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 通用异常处理
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result error(Exception e){
        e.printStackTrace();
        return Result.error();
    }

    /**
     * 指定异常处理
     * @param e
     * @return
     */
    @ExceptionHandler(NullPointerException.class)
    @ResponseBody
    public Result error(NullPointerException e){
        e.printStackTrace();
        return Result.setResult(ResultCodeEnum.PARAM_ERROR);
    }

    /**
     * 指定异常处理
     * @param e
     * @return
     */
    @ExceptionHandler(HttpClientErrorException.class)
    @ResponseBody
    public Result error(IndexOutOfBoundsException e){
        e.printStackTrace();
        return Result.setResult(ResultCodeEnum.PARAM_ERROR);
    }

    /**
     * 自定义异常处理方法
     * @param e
     * @return
     */
    @ExceptionHandler(CMSException.class)
    @ResponseBody
    public Result error(CMSException e){
        e.printStackTrace();
        return Result.error().message(e.getMessage()).code(e.getCode());
    }

}
